package cn.livingCloud.controller.core;

import cn.livingCloud.dto.*;
import cn.livingCloud.service.EmployeeService;
import cn.livingCloud.service.MemberService;
import cn.livingCloud.utils.BindingResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 员工控制器
 * Created by DingJie on 2016/5/30.
 */
@Controller
@RequestMapping("/admin/organization/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService service;
    @Autowired
    private MemberService memberService;


    @RequestMapping("/list.htm")
    public String loadList(EmployeeListDto listDto, Model model) {
        service.loadEmployeeListDto(listDto);
        model.addAttribute("listDto", listDto);
        return "organization/employee/list";
    }

    /**
     * 绑定员工和微信号
     */
    @RequestMapping("/bind_member.htm")
    public String bindMember(@RequestParam("employeeUuid") String employeeUuid, Model model) {
        EmployeeBindMemberDto formDto = service.loadBindMemberDto(employeeUuid);
        model.addAttribute("formDto", formDto);
        return "organization/employee/bind_member";
    }

    /**
     * 绑定员工和微信号 提交
     */
    @RequestMapping(value = "/bind_member.htm", method = RequestMethod.POST)
    public String bindMemberSubmit(@ModelAttribute("formDto") @Valid EmployeeBindMemberDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "organization/employee/bind_member";
        }
        service.bindMember(formDto);
        model.addAttribute("bind", "success");
        return "redirect:list.htm";
    }

    /**
     * 搜索相关会员
     */
    @RequestMapping("/load_member_json")
    @ResponseBody
    public MemberListDto searchMember(String key, Model model) {
        return memberService.findUnBoundMember(key);
    }

    /**
     * 添加/编辑
     */
    @RequestMapping("/edit.htm")
    public String edit(EmployeeFormDto formDto, Model model) {
        service.loadEmployeeFromDto(formDto);
        model.addAttribute("formDto", formDto);
        return "organization/employee/edit";
    }

    /**
     * 提交添加/编辑
     */
    @RequestMapping(value = "/edit.htm", method = RequestMethod.POST)
    public String edit(@ModelAttribute("formDto") @Valid EmployeeFormDto formDto, BindingResult result) {
        if (result.hasErrors()) {
            return "organization/employee/edit";
        }
        service.edit(formDto);
        if (formDto.isContinuation()) {
            return "redirect:edit.htm?edit=success";
        }
        return "redirect:list.htm?edit=success";
    }

    //删除
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    public String delete(String uuid, Model model) {
        service.delete(uuid);
        model.addAttribute("delete", "success");
        return "redirect:list.htm";
    }

    @RequestMapping("/find_employee")
    @ResponseBody
    public List<EmployeeJsonDto> findEmployees(String key, Model model) {
        return service.findEmployees(key);
    }

    /**
     * 解除绑定员工和微信号
     */
    @RequestMapping("/relieve_member")
    public String relieveMember(@RequestParam("employeeUuid") String employeeUuid, Model model) {
        EmployeeRelieveMemberDto formDto = service.loadEmployeeRelieveMemberDto(employeeUuid);
        model.addAttribute("formDto", formDto);
        return "organization/employee/relieve_member";
    }

    /**
     * 解除绑定员工和微信号 提交
     */
    @RequestMapping(value = "/relieve_member", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto relieveMemberSubmit(@ModelAttribute("formDto") @Valid EmployeeRelieveMemberDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.RelieveMember(formDto);
    }
}
